package com.jckj.core.config;

import com.jckj.core.shiro.MyRealm;
import com.jckj.core.util.MyUtils;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.cache.ehcache.EhCacheManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.cache.ehcache.EhCacheManagerFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author liuzhiyuan
 *  shiro configuration
 */
@Configuration
public class ShiroConfig {

    private static String SHIRO_RULE_LOCATION = "shiro.properties";
    private static String SHIRO_CACHE_LOCATION = "ehcache.xml";
    private static String AUTHENTICATION_CACHE_NAME = "shiro-ehcache";
    private static String AUTHORIZATION_CACHE_NAME = "shiro-ehcache";

    @Bean
    public EhCacheCacheManager cacheManager(){
        return new EhCacheCacheManager(ehcache().getObject());

    }

    @Bean
    public EhCacheManagerFactoryBean ehcache(){
        EhCacheManagerFactoryBean ehCacheManagerFactoryBean = new EhCacheManagerFactoryBean();
        ehCacheManagerFactoryBean.setConfigLocation(new ClassPathResource(SHIRO_CACHE_LOCATION));
        //解决共用问题
        ehCacheManagerFactoryBean.setShared(true);
        return ehCacheManagerFactoryBean;
    }

    @Bean(name="shiroFilter")
    public ShiroFilterFactoryBean shiroFilter() {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager());
        shiroFilter.setLoginUrl("/login");
        //配置过滤规则
        Map<String, String> filterChainMap = MyUtils.convertPropertiesToMap(SHIRO_RULE_LOCATION);
        shiroFilter.setFilterChainDefinitionMap(filterChainMap);
        Map<String, Filter> filters = new HashMap<String, Filter>();
        shiroFilter.setFilters(filters);
        return shiroFilter;
    }

    @Bean
    public DefaultWebSecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
        DefaultWebSessionManager sessionManager = new DefaultWebSessionManager();
        sessionManager.setSessionIdCookie(simpleCookie());
        securityManager.setSessionManager(sessionManager);
        EhCacheManager ehCacheManager = new EhCacheManager();
        ehCacheManager.setCacheManager(ehcache().getObject());
        securityManager.setCacheManager(ehCacheManager);
        securityManager.setRealm(realm());
        return securityManager;
    }

    @Bean
    public SimpleCookie simpleCookie(){
        SimpleCookie simpleCookie = new SimpleCookie();
        simpleCookie.setName("shiro.sesssion");
        simpleCookie.setPath("/");
        return simpleCookie;
    }

    @Bean
    public MyRealm realm() {
        MyRealm realm = new MyRealm();
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        hashedCredentialsMatcher.setHashAlgorithmName("md5");

        realm.setAuthenticationCacheName(AUTHENTICATION_CACHE_NAME);
        realm.setAuthorizationCacheName(AUTHORIZATION_CACHE_NAME);
        return realm;
    }


    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(){
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager());
        return advisor;
    }
}
